package com.iflytek.exam.service.impl;

import java.util.List;

import com.iflytek.common.core.domain.entity.SysUser;
import com.iflytek.common.utils.DateUtils;
import com.iflytek.exam.domain.TbAll;
import com.iflytek.exam.mapper.TbAllMapper;
import com.iflytek.system.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.iflytek.exam.mapper.TbQuestionSummaryMapper;
import com.iflytek.exam.domain.TbQuestionSummary;
import com.iflytek.exam.service.ITbQuestionSummaryService;

/**
 * 题库管理Service业务层处理
 *
 * @author Dogdog
 * @date 2024-07-06
 */
@Service
public class TbQuestionSummaryServiceImpl implements ITbQuestionSummaryService
{
    @Autowired
    private TbQuestionSummaryMapper tbQuestionSummaryMapper;

    @Autowired
    private SysUserServiceImpl sysUserService;

    @Autowired
    private TbAllMapper tbAllMapper;

    /**
     * 查询题库管理
     *
     * @param id 题库管理主键
     * @return 题库管理
     */
    @Override
    public TbQuestionSummary selectTbQuestionSummaryById(Long id)
    {
        return tbQuestionSummaryMapper.selectTbQuestionSummaryById(id);
    }

    /**
     * 查询题库管理列表
     *
     * @param tbQuestionSummary 题库管理
     * @return 题库管理
     */
    @Override
    public List<TbQuestionSummary> selectTbQuestionSummaryList(TbQuestionSummary tbQuestionSummary)
    {
        return tbQuestionSummaryMapper.selectTbQuestionSummaryList(tbQuestionSummary);
    }

    /**
     * 新增题库管理
     *
     * @param tbQuestionSummary 题库管理
     * @return 结果
     */
    @Override
    public int insertTbQuestionSummary(TbQuestionSummary tbQuestionSummary)
    {
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        System.out.println("______" + name);
        Long userId = sysUserService.selectUserByUserName(name).getUserId();
        System.out.println("++++" + userId);
        tbQuestionSummary.setCreateTime(DateUtils.getNowDate());
        tbQuestionSummary.setUserId(userId);
        return tbQuestionSummaryMapper.insertTbQuestionSummary(tbQuestionSummary);
    }

    /**
     * 修改题库管理
     *
     * @param tbQuestionSummary 题库管理
     * @return 结果
     */
    @Override
    public int updateTbQuestionSummary(TbQuestionSummary tbQuestionSummary)
    {
        tbQuestionSummary.setUpdateTime(DateUtils.getNowDate());
        return tbQuestionSummaryMapper.updateTbQuestionSummary(tbQuestionSummary);
    }/**
     * 增加题库数量
     *
     * @param tbQuestionSummary 增加题库数量
     * @return 结果
     */
    @Override
    public int addTbQuestionSummary(TbQuestionSummary tbQuestionSummary)
    {
        tbQuestionSummary.setUpdateTime(DateUtils.getNowDate());
        return tbQuestionSummaryMapper.addTbQuestionSummary(tbQuestionSummary);
    }

    /**
     * 批量删除题库管理
     *
     * @param ids 需要删除的题库管理主键
     * @return 结果
     */
    @Override
    public int deleteTbQuestionSummaryByIds(Long[] ids)
    {
        for (int i = 0; i < ids.length; i++){
            TbAll tbAll = tbAllMapper.selectTbAllById(ids[i]);
            //如果tbAll不为空返回,说明题库下还有数据，不让删除
            if (tbAll != null){
                return 10000;
            }
        }
        //删除题库，有题提示有级联的题不能删除
        return tbQuestionSummaryMapper.deleteTbQuestionSummaryByIds(ids);
    }

    /**
     * 删除题库管理信息
     *
     * @param id 题库管理主键
     * @return 结果
     */
    @Override
    public int deleteTbQuestionSummaryById(Long id)
    {
        return tbQuestionSummaryMapper.deleteTbQuestionSummaryById(id);
    }

    @Override
    public List<TbQuestionSummary> selectTbChoiceList(TbQuestionSummary tbQuestionSummary) {
        return null;
    }
}
